package pl.edu.agh.uddiProxy.dao;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.providers.ProviderManager;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.dao.DaoAuthenticationProvider;
import org.springframework.security.providers.encoding.PasswordEncoder;

import pl.edu.agh.uddiProxy.AuthenticationHelper;
import pl.edu.agh.uddiProxy.UDDIClient;
import pl.edu.agh.uddiProxy.types.ParametersMap;
import pl.edu.agh.uddiProxy.types.ServiceDynamicParameters;

public class DAOHelper {
	private static DynamicParameterDAO dynamicParameterDAO;
	private static UserDAO userDAO;
	private static UDDIClient client;
	private static ParametersMap parametersMap;
	private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "context.xml" });
	private static boolean isUpdated;
	private static Logger logger = Logger.getLogger(DAOHelper.class);
	private static DaoAuthenticationProvider authenticationProvider;
	private static PasswordEncoder passwordEncoder;
	private static ProviderManager authenticationManager;
	
	public static DynamicParameterDAO getDynamicParameterDAO() {
		if (dynamicParameterDAO == null) {
			dynamicParameterDAO = (DynamicParameterDAO) context.getBean("DynamicParameterDAO");
		}
		return dynamicParameterDAO;
	}
	
	public static ProviderManager getAuthenticationManager() {
		if (authenticationManager == null) {
			authenticationManager = (ProviderManager) context.getBean("authenticationManager");
		}
		return authenticationManager;
	}
	
	public static UserDAO getUserDAO() {
		if (userDAO == null) {
			userDAO = (UserDAO) context.getBean("UserDAO");
		}
		return userDAO;
	}
	
	public static DaoAuthenticationProvider getAuthenticationProvider() {
		if (authenticationProvider == null) {
			authenticationProvider = (DaoAuthenticationProvider) context.getBean("daoAuthenticationProvider");
		}
		return authenticationProvider;
	}
	
	public static PasswordEncoder getPasswordEncoder() {
		if (passwordEncoder == null) {
			passwordEncoder = (PasswordEncoder) context.getBean("passwordEncoder");
		}
		return passwordEncoder;
	}
	
	public static void test()  {
		context = new ClassPathXmlApplicationContext(new String[] { "context.xml", "context-test.xml" });
		dynamicParameterDAO = (DynamicParameterDAO) context.getBean("DynamicParameterDAO");
		userDAO = (UserDAO) context.getBean("UserDAO");
	}
	
	public static UDDIClient getUDDIClient() {
		if (client == null) {
			client = (UDDIClient) context.getBean("UDDIClient");
		}
		return client;
	}
	
	public static ParametersMap getParametersMap() {
		if (parametersMap == null) {
			if (context == null) {
				return null;
			}
			parametersMap = (ParametersMap) context.getBean("ParametersMap");
			if (!isUpdated) {
				logger.info("Updating parameters");
				parametersMapChange();
				logger.info("Parameters updated");
			}
		}
		return parametersMap;
	}
	
	private static void parametersMapChange() {
		UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("admin", "admin");
		AuthenticationHelper.login(authentication);
		
		DynamicParameterDAO parameterDAO = getDynamicParameterDAO();
		for (ServiceDynamicParameters serviceDynamicParameters : parameterDAO.getAll()) {
			serviceDynamicParameters.initMap();
			parameterDAO.update(serviceDynamicParameters);
		}
		isUpdated = true;
		AuthenticationHelper.logout(authentication);
	}
}
